package net.portly.cst;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.portly.cst.flex.FlexFamily;
import net.portly.cst.flex.FlexItemType;
import net.portly.cst.page.PageAssetsContainer;
import net.portly.cst.tools.ArgsManager;

public class ATDConsolidationActivity {
	private List<FlexFamily> flexFamilies = new ArrayList<FlexFamily>();
	private List<AssetTypeDescriptor> assetTypeDescriptors = null;
	private PageAssetsContainer pageAssetsContainer = null;
	private ArgsManager argsManager;

	public ATDConsolidationActivity(List<AssetTypeDescriptor> atds, ArgsManager argsManager) {
		assetTypeDescriptors = atds;
		this.argsManager = argsManager;
	}

	public List<FlexFamily> consolidateAssetTypeDescriptorsForFlex() {
		// * set up the attributes first
		for (AssetTypeDescriptor assetTypeDescriptor : assetTypeDescriptors) {
			if (assetTypeDescriptor.getWhatTypeOfFlexItem() == FlexItemType.ATTRIBUTE) {
				FlexFamily ff = new FlexFamily();
				ff.putTypeWithAssetTypeDescriptor(assetTypeDescriptor);
				flexFamilies.add(ff);
			}
		}

		// * Add all others
		for (AssetTypeDescriptor atd : assetTypeDescriptors) {
			if (atd.getWhatTypeOfFlexItem() != FlexItemType.ATTRIBUTE && atd.getWhatTypeOfFlexItem() != null
					&& !(atd.getWhatTypeOfFlexItem().name().startsWith("PAGE"))) {
				// * Find matching attribute
				for (FlexFamily ff : flexFamilies) {
					if (ff.getAssetTypeDescriptorForFlexItem(FlexItemType.ATTRIBUTE).getAssettype().equals(atd.getAttr())) {
						ff.putTypeWithAssetTypeDescriptor(atd);
					}
				}
			}
		}

		return flexFamilies;
	}

	public PageAssetsContainer consolidateAssetTypeDescriptorsForPage() {
		pageAssetsContainer = new PageAssetsContainer();

		// * Add all others - including pages
		for (AssetTypeDescriptor atd : assetTypeDescriptors) {
			if (atd.getWhatTypeOfFlexItem() != null && atd.getWhatTypeOfFlexItem().name().startsWith("PAGE")) {
				pageAssetsContainer.add(atd);
				pageAssetsContainer.putTypeWithAssetTypeDescriptor(atd);
			}
		}

		// In some instances, PageDef/Attr/Filter is not being represented as
		// an ASSET_TYPE entry in the CSDT bundle, so we may have to force it in
		// there in order to collect the actual Assets that are in the ASSET
		// directory. This only happens if we have asked to force those in there
		if (argsManager != null && argsManager.getForcePageInclusion()) {
			FlexItemType fiType[] = { FlexItemType.PAGEATTR, FlexItemType.PAGEDEF, FlexItemType.PAGEFILTER };

			for (FlexItemType fit : fiType) {
				if (pageAssetsContainer.getAssetTypeDescriptorForFlexItem(fit) == null) {
					pageAssetsContainer.putTypeWithAssetTypeDescriptor(newAssetTypeDescriptor(fit));
				}
			}
		}
		return pageAssetsContainer;
	}

	private AssetTypeDescriptor newAssetTypeDescriptor(FlexItemType flexItemType) {
		Map<String, String> metaDataMap = new HashMap<String, String>();
		metaDataMap.put("id", "-1");
		metaDataMap.put("fw_uid", "-1");
		metaDataMap.put("assettype", flexItemType.getType());
		metaDataMap.put("complexAssetClass", flexItemType.getComplexAssetClass());
		return new AssetTypeDescriptor(metaDataMap);
	}

}
